<?php


namespace App\Admin\Controllers;


use App\Admin\Forms\Order;
use App\Models\AdminConfig;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Card;

class OrderConfigController extends AdminController
{
    /**
     * 参数设置
     * @Author MrGuo <gj2517@qq.com>
     */
    protected function setItems(Content $content) {
        return $content->title('订单配置')
            ->body(new Card(new Order()));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form() {
        return Form::make(new AdminConfig(), function (Form $form) {
            $form->display('id');
            if ($form->model()->id) {
                $form->text('name')->help("由英文字母和下划线组成，如 <code>site_copyright</code>，调用方法：<code>sys_config('site_copyright')</code>")
                    ->placeholder("创建后不可修改")
                    ->disable();
            } else {
                $form->text('name')->help("由英文字母和下划线组成，如 <code>site_copyright</code>，调用方法：<code>sys_config('site_copyright')</code>")
                    ->placeholder("创建后不可修改")
                    ->rules("required|unique:admin_config");
            }

            $form->text('title')->rules("required");
            $form->select('type')->rules('required')
                ->options(AdminConfig::getInputType());

            $form->select('group')
                ->options(AdminConfig::getConfigGroup());
            $form->textarea('options')
                ->help("格式: value:label , 每行一个, 英文逗号分隔")
                ->placeholder("单选多选的选项")
                ->rules('max:255');
            $form->text('placeholder');
            $form->text('help');
            $form->text('sort')->default(100);
            $form->display('env')
                ->help("由于会直接修改.env配置，请在数据库修改")
                ->disable();
            $form->text('rules');
            $form->radio('status')
                ->options(["1" => "启用", "0" => "禁用"])->default(1);

            $form->saved(function (Form $form, $result) {
                // 保存问了以后更新缓存中的数据
                AdminConfig::loadConfig($form->model()->name);
            });
        });
    }
}
